﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LabMonitorRemote
{
    class DewkSettingsConfiguration
    {
        /* CLASS IS USED TO REPRESENT THE SETTINGS STORED ON THE DEWK. 
         * This should not be used to represent the database stored records.
         * Use this class to temporarially stored the values from a Dewk or before pushing settings to a Dewk.
         */

        public DewkSettingsConfiguration()
        {
        }

        public DewkSettingsConfiguration(DewkLogger Dewk)
        {
            this.Dewk = Dewk;
            this.DataRecordingTimer = Dewk.RecordingInterval;
            this.DisplayF = Dewk.DisplayF;
            
            #region If Channel 1 Enabled
            if (Dewk.Ch1Enabled)
            {
                this.Channel1Enabled = Dewk.Ch1Enabled;
                this.Channel1TempLowerAlarmEnabled = true;
                this.Channel1TempUpperAlarmEnabled = true;
                this.Channel1HumidityLowerAlarmEnabled = true;
                this.Channel1HumidityUpperAlarmEnabled = true;

                this.Channel1TempLowerLimit = Dewk.Ch1TempLow;
                this.Channel1TempUpperLimit = Dewk.Ch1TempHigh;
                this.Channel1HumidityLowerLimit = Dewk.Ch1HumidLow;
                this.Channel1HumidityUpperLimit = Dewk.Ch1HumidHigh;
            }
            else
            {
                this.Channel1Enabled = Dewk.Ch1Enabled;
                this.Channel1TempLowerAlarmEnabled = false;
                this.Channel1TempUpperAlarmEnabled = false;
                this.Channel1HumidityLowerAlarmEnabled = false;
                this.Channel1HumidityUpperAlarmEnabled = false;

                this.Channel1TempLowerLimit = 0;
                this.Channel1TempUpperLimit = 100;
                this.Channel1HumidityLowerLimit = 0;
                this.Channel1HumidityUpperLimit = 100;
            }
            #endregion If Channel 1 Enabled
            #region If Channel 2 Enabled
            if (Dewk.Ch2Enabled)
            {
                this.Channel2Enabled = Dewk.Ch2Enabled;
                this.Channel2TempLowerAlarmEnabled = true;
                this.Channel2TempUpperAlarmEnabled = true;
                this.Channel2HumidityLowerAlarmEnabled = true;
                this.Channel2HumidityUpperAlarmEnabled = true;

                this.Channel2TempLowerLimit = Dewk.Ch2TempLow;
                this.Channel2TempUpperLimit = Dewk.Ch2TempHigh;
                this.Channel2HumidityLowerLimit = Dewk.Ch2HumidLow;
                this.Channel2HumidityUpperLimit = Dewk.Ch2HumidHigh;
            }
            else
            {
                this.Channel2Enabled = Dewk.Ch2Enabled;
                this.Channel2TempLowerAlarmEnabled = false;
                this.Channel2TempUpperAlarmEnabled = false;
                this.Channel2HumidityLowerAlarmEnabled = false;
                this.Channel2HumidityUpperAlarmEnabled = false;

                this.Channel2TempLowerLimit = 0;
                this.Channel2TempUpperLimit = 100;
                this.Channel2HumidityLowerLimit = 0;
                this.Channel2HumidityUpperLimit = 100;
            }
            #endregion If Channel 2 Enabled
        }

        #region Properties
        public DewkLogger Dewk
        {
            get;
            set;
        }
        public Int32 DataRecordingTimer
        {
            get;
            set;
        }
        public Boolean DisplayF
        {
            get;
            set;
        }
        
        public Boolean Channel1Enabled
        {
            get;
            set;
        }
        public Boolean Channel2Enabled
        {
            get;
            set;
        }
        public Boolean Channel1TempUpperAlarmEnabled
        {
            get;
            set;
        }
        public Boolean Channel1TempLowerAlarmEnabled
        {
            get;
            set;
        }
        public Boolean Channel1HumidityUpperAlarmEnabled
        {
            get;
            set;
        }
        public Boolean Channel1HumidityLowerAlarmEnabled
        {
            get;
            set;
        }
        public Boolean Channel2TempUpperAlarmEnabled
        {
            get;
            set;
        }
        public Boolean Channel2TempLowerAlarmEnabled
        {
            get;
            set;
        }
        public Boolean Channel2HumidityUpperAlarmEnabled
        {
            get;
            set;
        }
        public Boolean Channel2HumidityLowerAlarmEnabled
        {
            get;
            set;
        }
        public Double Channel1TempUpperLimit
        {
            get;
            set;
        }
        public Double Channel1TempLowerLimit
        {
            get;
            set;
        }
        public Double Channel1HumidityUpperLimit
        {
            get;
            set;
        }
        public double Channel1HumidityLowerLimit
        {
            get;
            set;
        }
        public Double Channel2TempUpperLimit
        {
            get;
            set;
        }
        public Double Channel2TempLowerLimit
        {
            get;
            set;
        }
        public Double Channel2HumidityUpperLimit
        {
            get;
            set;
        }
        public double Channel2HumidityLowerLimit
        {
            get;
            set;
        }
        #endregion Properties

        private Double DecodeDoubleValue(byte[] array)
        {
            Double value;
            char[] dewkReplyChars = System.Text.Encoding.ASCII.GetChars(array);
            string dewkReplyString = "";
            for (Int32 i = 0; i < dewkReplyChars.Length; i++)
            {
                dewkReplyString += array[i].ToString();
            }
            value = Convert.ToDouble(dewkReplyString);
            return value;
        }
        private Boolean DecodeBooleanValue(byte[] array)
        {
            Boolean value = false;

            return value;
        }

        private Int32 GetInt32(Byte[] buffer, Int32 offset, Int32 length)
        {
            /* ASCII: 48 49 50 51 52 53 54 55 56 57
             * Char:   1  2  3  4  5  6  7  8  9  0
             * Value transfered from LSD to MSD.
            */
            Int32 i = 0;
            while ((buffer[offset] >= 48) && (buffer[offset] <= 57) && (offset < length))
                i = 10 * i + (buffer[offset++] - 48);
            return i;
        }

        private Double GetDouble(Byte[] array)
        {
            Double value;
            char[] dewkReplyChars = System.Text.Encoding.ASCII.GetChars(array);
            string dewkReplyString = "";
            for (Int32 i = 0; i < dewkReplyChars.Length; i++)
            {
                dewkReplyString += array[i].ToString();
            }
            value = Convert.ToDouble(dewkReplyString);
            return value;
        }

        private void MakeBeep(string IpAddress)
        {
            MakeDewkBeep beeper = new MakeDewkBeep(IpAddress);
            beeper.WriteCommand();
            beeper.ManuallyCloseClient();
        }

        public void GetConfiguration()
        {
            // use Get DewkCommands and linked reads. Interpret values and store in properties
        }
        public void SetConfiguration()
        {
            // Use Set DewkCommands to load property values onto Dewk.

            // Set Current Time & Date
            SetDate setDate = new SetDate(Dewk.IpAddress);
            setDate.WriteCommand();
            setDate.ManuallyCloseClient();

            SetTime setTime = new SetTime(Dewk.IpAddress);
            setTime.WriteCommand();
            setTime.ManuallyCloseClient();

            MakeBeep(Dewk.IpAddress);

            // Set Display Unit
            SetDisplayUnit setDisplayUnit;
            if (Dewk.DisplayF)
            {
                setDisplayUnit = new SetDisplayUnit(Dewk.IpAddress, "F");
            }
            else
            {
                setDisplayUnit = new SetDisplayUnit(Dewk.IpAddress, "C");
            }
            setDisplayUnit.WriteCommand();
            setDisplayUnit.ManuallyCloseClient();

            MakeBeep(Dewk.IpAddress);

            // Set Channels enabled or Not
            SetChannelEnabled setChannel1Enabled = new SetChannelEnabled(Dewk.IpAddress, 1, this.Channel1Enabled);
            setChannel1Enabled.WriteCommand();
            setChannel1Enabled.ManuallyCloseClient();

                // re-instance for ch 2
            SetChannelEnabled setChannel2Enabled = new SetChannelEnabled(Dewk.IpAddress, 2, this.Channel2Enabled);
            setChannel2Enabled.WriteCommand();
            setChannel2Enabled.ManuallyCloseClient();

            MakeBeep(Dewk.IpAddress);

            // Set Data Recording Timer
            SetDataRecordingTimer setDataRecTimer = new SetDataRecordingTimer(Dewk.IpAddress, this.DataRecordingTimer);
            setDataRecTimer.WriteCommand();
            setDataRecTimer.ManuallyCloseClient();

            MakeBeep(Dewk.IpAddress);

            #region If Channel 1 Enabled
            if (this.Channel1Enabled)
            {
                /* COMMAND OBJECTS ARE INSTANCED WITH REQUIRED INFO
                 * DewkCommand.WriteCommand() writes the specific command to the network stream
                 * DewkCommand.ManuallyCloseClient() is called to terminate network connection before next command object is instanced.
                 */

                // Set Alarms Enabled (if Channel is enabled)
                    // High
                SetTemperatureHighAlarmEnabled setTempHighAlarmEnabled = new SetTemperatureHighAlarmEnabled(Dewk.IpAddress, 1, this.Channel1TempUpperAlarmEnabled);
                setTempHighAlarmEnabled.WriteCommand();
                setTempHighAlarmEnabled.ManuallyCloseClient();
                    // Low
                SetTemperatureLowAlarmEnabled setTempLowAlarmEnabled = new SetTemperatureLowAlarmEnabled(Dewk.IpAddress, 1, this.Channel1TempLowerAlarmEnabled);
                setTempLowAlarmEnabled.WriteCommand();
                setTempLowAlarmEnabled.ManuallyCloseClient();                   

                // Set Upper Temp Alarm Limits (if channel is enabled)
                SetUpperTemperatureLimit setUppTempLimit = new SetUpperTemperatureLimit(Dewk.IpAddress, 1, this.Channel1TempUpperLimit);
                setUppTempLimit.WriteCommand();
                setUppTempLimit.ManuallyCloseClient();

                // Set Lower Temp Alarm Limits (if channel is enabled)
                SetLowerTemperatureLimit setLowTempLimit = new SetLowerTemperatureLimit(Dewk.IpAddress, 1, this.Channel1TempLowerLimit);
                setLowTempLimit.WriteCommand();
                setLowTempLimit.ManuallyCloseClient();

                // Set Upper Humidity Alarm Limits (if channel is enabled)
                SetUpperHumidityLimit setUppHumidLimit = new SetUpperHumidityLimit(Dewk.IpAddress, 1, this.Channel1HumidityUpperLimit);
                setUppHumidLimit.WriteCommand();
                setUppHumidLimit.ManuallyCloseClient();

                // Set Lower Humidity Alarm Limits (if channel is enabled)
                SetLowerHumidityLimit setLowHumidLimit = new SetLowerHumidityLimit(Dewk.IpAddress, 1, this.Channel1HumidityLowerLimit);
                setLowHumidLimit.WriteCommand();
                setLowHumidLimit.ManuallyCloseClient();

                MakeBeep(Dewk.IpAddress);
            }
            else 
            {
                // disable 
            }
            #endregion If Channel 1 Enabled

            #region If Channel 2 Enabled
            if (this.Channel2Enabled)
            {
                // Set Alarms Enabled (if Channel is enabled)
                // High
                SetTemperatureHighAlarmEnabled setTempHighAlarmEnabled = new SetTemperatureHighAlarmEnabled(Dewk.IpAddress, 2, this.Channel2TempUpperAlarmEnabled);
                setTempHighAlarmEnabled.WriteCommand();
                setTempHighAlarmEnabled.ManuallyCloseClient();
                // Low
                SetTemperatureLowAlarmEnabled setTempLowAlarmEnabled = new SetTemperatureLowAlarmEnabled(Dewk.IpAddress, 2, this.Channel2TempLowerAlarmEnabled);
                setTempLowAlarmEnabled.WriteCommand();
                setTempLowAlarmEnabled.ManuallyCloseClient();

                // Set Upper Temp Alarm Limits (if channel is enabled)
                SetUpperTemperatureLimit setUppTempLimit = new SetUpperTemperatureLimit(Dewk.IpAddress, 2, this.Channel2TempUpperLimit);
                setUppTempLimit.WriteCommand();
                setUppTempLimit.ManuallyCloseClient();

                // Set Lower Temp Alarm Limits (if channel is enabled)
                SetLowerTemperatureLimit setLowTempLimit = new SetLowerTemperatureLimit(Dewk.IpAddress, 2, this.Channel2TempLowerLimit);
                setLowTempLimit.WriteCommand();
                setLowTempLimit.ManuallyCloseClient();

                // Set Upper Humidity Alarm Limits (if channel is enabled)
                SetUpperHumidityLimit setUppHumidLimit = new SetUpperHumidityLimit(Dewk.IpAddress, 2, this.Channel2HumidityUpperLimit);
                setUppHumidLimit.WriteCommand();
                setUppHumidLimit.ManuallyCloseClient();

                // Set Lower Humidity Alarm Limits (if channel is enabled)
                SetLowerHumidityLimit setLowHumidLimit = new SetLowerHumidityLimit(Dewk.IpAddress, 2, this.Channel2HumidityLowerLimit);
                setLowHumidLimit.WriteCommand();
                setLowHumidLimit.ManuallyCloseClient();

                MakeBeep(Dewk.IpAddress);
            }
            else
            {
                // disable
            }
            #endregion If Channel 2 Enabled
        } 

    }
}
